package com.zcx;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

/**
 * @author zhaochangxin
 * @Title: AutoCode
 * @Package com.zcx
 * @Description: 测试mybatisPlus自动生成
 * @date 2022/3/30 14:28
 * 官网配置：https://baomidou.com/pages/981406/#%E7%AD%96%E7%95%A5%E9%85%8D%E7%BD%AE-strategyconfig
 */
public class AutoCode {
    public static void main(String[] args) {
        String url = "jdbc:mysql://192.168.90.20:3306/qsotc_zcx_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8";

        /*数据库配置*/
        DataSourceConfig dataSourceConfig =
                new DataSourceConfig
                        .Builder(url, "qsotc", "waOzHW[Jphgi10bb")
                        .dbQuery(new MySqlQuery())
                        .build();


        /*创建一共代码生成器对象*/
        AutoGenerator generator = new AutoGenerator(dataSourceConfig);
        // 当前的项目路径
        String property = System.getProperty("user.dir");

        /*全局配置*/
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .fileOverride()
                .outputDir(property + "/mybatisplus/src/main/java")
                .author("zcx")
//                .enableKotlin()
//                    .enableSwagger()
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .build();

        generator.global(globalConfig);

        /*包配置(PackageConfig)*/

        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.zcx")
                .entity("pojo")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
//                    .xml("mapper.xml")
                .controller("controller")
//                    .other("other")
//                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"))
                .build();

        generator.packageInfo(packageConfig);

        TemplateConfig templateConfig=new TemplateConfig.Builder()
                .controller("/templates/controller.java.vm")
                .build();
        generator.template(templateConfig);


        /*策略配置*/
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                // enableCapitalMode	开启大写命名	默认值:false
                .enableCapitalMode()
                // enableSkipView	开启跳过视图	默认值:false
                .enableSkipView()
                // disableSqlFilter	禁用 sql 过滤	默认值:true，语法不能支持使用 sql 过滤表的话，可以考虑关闭此开关
                .disableSqlFilter()
                /*
                addInclude(String...)	增加表匹配(内存过滤)	include 与 exclude 只能配置一项
                addExclude(String...)	增加表排除匹配(内存过滤)	include 与 exclude 只能配置一项
                .addInclude("t_role","t_role_menus","t_role_user_menus")
                .addTablePrefix("t_", "c_")
                 */
                .addInclude("test_template")
                .addTablePrefix("platform_","intern_","test")
//                .addFieldSuffix("_flag")
                /*   .entityBuilder()
                   .controllerBuilder()
                   .mapperBuilder()
                   .serviceBuilder()*/
                .build();

        strategyConfig.entityBuilder()
                // enableLombok	开启 lombok 模型	默认值:false
                .enableLombok()
                // enableRemoveIsPrefix	开启 Boolean 类型字段移除 is 前缀	默认值:false
                .enableRemoveIsPrefix()
                // versionColumnName(String)	乐观锁字段名(数据库)
                .versionColumnName("version")
                // versionPropertyName(String)	乐观锁属性名(实体)
                .versionPropertyName("version")
                // logicDeleteColumnName(String)	逻辑删除字段名(数据库)
                .logicDeleteColumnName("deleted")
                // logicDeletePropertyName(String)	逻辑删除属性名(实体)
                .logicDeletePropertyName("deleted")
//                    .naming(NamingStrategy.no_change)
//                    .columnNaming(NamingStrategy.underline_to_camel)
//                .addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")
//                .addIgnoreColumns("age")
                .addTableFills(new Column("created_date", FieldFill.INSERT))
                .addTableFills(new Property("last_modified", FieldFill.INSERT_UPDATE))
                .build();


        strategyConfig
                .mapperBuilder()
                .superClass(BaseMapper.class)
                .enableMapperAnnotation()
                .build();

        generator.strategy(strategyConfig);


        /*执行代码生成器*/
        generator.execute();


    }
}
